package com.ioudi.digu.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ioudi.digu.dao.support.IQuerySupport;
import com.ioudi.digu.service.support.ListResult;
import com.ioudi.digu.vo.Location;
import com.ioudi.digu.vo.ProducedMessage;

public class MessageDAO extends HibernateDaoSupport
{
	private IQuerySupport querySupport;
	
	public ProducedMessage getMessage(String messageId)
    {
		ProducedMessage msg = null;
        Session s = this.getSession();
        try
        {
        	s.beginTransaction();
        	Object o = s.createQuery("from ProducedMessage msg where msg.messageId=:messageId").
        	    setString("messageId", messageId).uniqueResult();
        	if(o != null)
        	{
        		msg = (ProducedMessage)o;
        	}
        }
        catch(Exception e)
        {
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
        return msg;
    }
	
	 public boolean saveMessage(ProducedMessage message)
     {
    	boolean result = false;
    	if(message == null)
    	{
    		return result;
    	}
        Session s = this.getSession();
        try
        {
        	s.beginTransaction();
        	s.merge(message);
        	s.getTransaction().commit();
        	result = true;
        }
        catch(Exception e)
        {
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
        return result;
    }
	 
	public boolean deleteMessages(String[] messageIds)
	{
		boolean result = false;
        Session s = this.getSession();
        s.beginTransaction();
        try
        {
        	int i = messageIds.length - 1;
        	String sql = "delete ProducedMessage pm where pm.messageId=:messageId";
        	while(i >= 0)
        	{
        		s.createQuery(sql).setString("messageId", messageIds[i]).executeUpdate();
        	}
        	s.getTransaction().commit();
        	result = true;
        }
        catch(Exception e)
        {
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
        return result;
	}
	
	public ListResult findMessage(int index, int maxCount, Map<String, Object> condition)
	{
		String sql = "from ProducedMessage pm ";
		List<String> keys = new ArrayList<String>();
		String statement = querySupport.createStatement("pm", condition, keys);
		sql += (statement.length() > 0) ? "where " + statement : "";
		sql += " order by pm.timestamp desc";
        Session s = this.getSession();
        s.beginTransaction();
        ListResult result = new ListResult();
        try
        {
        	Query query = s.createQuery("select count(pm) " + sql);
        	Object temp = querySupport.createQuery(query, keys, condition).uniqueResult();
        	int total = 0;
        	if(temp != null)
        	{
        		total = ((Long)temp).intValue();
        	}
        	result.setTotal(total);
        	query = s.createQuery(sql);
        	query = querySupport.createQuery(query, keys, condition);
        	query.setFirstResult(index);
        	if(maxCount > 0)
        	{
        		query.setMaxResults(maxCount);
        	}
        	result.setContents(query.list());
        	s.getTransaction().commit();
        }
        catch(Exception e)
        {
        	result = null;
        	s.getTransaction().rollback();
        }
        finally
        {
        	s.close();
        }
		return result;
	}
	
	public IQuerySupport getQuerySupport()
	{
		return querySupport;
	}

	public void setQuerySupport(IQuerySupport querySupport)
	{
		this.querySupport = querySupport;
	}

}
